package tech.xs.common.framework.mq;

import lombok.SneakyThrows;
import org.apache.commons.io.serialization.ValidatingObjectInputStream;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

public abstract class MqMessageListener<T> implements MessageListener {


    @SneakyThrows
    @Override
    public void onMessage(Message message) {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(message.getBody());
        SerialObjectInputStream objectInputStream = new SerialObjectInputStream(byteInputStream);
        T obj = (T) objectInputStream.readObject();
        objectInputStream.close();
        byteInputStream.close();
        onMessage(obj);
    }


    public abstract String getQueueName();


    public abstract void onMessage(T message) throws Exception;


}
